package com.android.tianyuanyouxuan.base;

import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.tianyuanyouxuan.util.TitleBarAction;
import com.hjq.bar.TitleBar;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;

import org.simple.eventbus.EventBus;

/**
 * 基类
 */
public class BaseActivity<P extends IPresenter> extends AppCompatActivity implements TitleBarAction, IView<P> {

    /** 标题栏对象 */
    protected TitleBar mTitleBar;

    protected P mPresenter;

    private LoadingPopupView loadingPopupView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingPopupView = new XPopup.Builder(this).asLoading();
        EventBus.getDefault().register(this);
    }

    protected void initTitleBar() {
        if (getTitleBar() != null) {
            getTitleBar().setOnTitleBarListener(this);
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
    @Override
    @Nullable
    public TitleBar getTitleBar() {
        if (mTitleBar == null) {
            mTitleBar = obtainTitleBar(getContentView());
        }
        return mTitleBar;
    }

    @Override
    public void onLeftClick(TitleBar titleBar) {
        finish();

    }

    /**
     * 和 setContentView 对应的方法
     */
    public ViewGroup getContentView() {
        return findViewById(Window.ID_ANDROID_CONTENT);
    }
}
